﻿/*
			       ___ _      _   ___ _  _    ___  _  _ 
			      | __| |    /_\ / __| || |  / _ \| \| |
			      | _|| |__ / _ \\__ \ __ | | (_) | .` |
			      |_| |____/_/ \_\___/_||_|  \___/|_|\_|
			                                            
			  _____ _  _   _ _____   ___ ___ _____ ___ _  _ 
			 |_   _| || | /_\_   _| | _ )_ _|_   _/ __| || |
			   | | | __ |/ _ \| |   | _ \| |  | || (__| __ |
			   |_| |_||_/_/ \_\_|   |___/___| |_| \___|_||_|
			        F L A S H  O N  T H A T  B I T C H
			        
		+--------------------------------------------------------+
		|	This file is delivered as is. Any modifications to   |
		|   this class and/or sub classes may cause errors and   |
		|   can cause you compilations to fail. FOTB takes no	 |
		|   responsibility for any complications caused by the	 |
		|   following code or any modified versions of it.		 |
		+--------------------------------------------------------+

*/

package com.flashonthatbitch.filesystem.parsers {

/*
		 ___ __  __ ___  ___  ___ _____ 
		|_ _|  \/  | _ \/ _ \| _ \_   _|
		 | || |\/| |  _/ (_) |   / | |  
		|___|_|  |_|_|  \___/|_|_\ |_|  
		I M P O R T
*/
	
	import com.flashonthatbitch.filesystem.FileObject;
	
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;

	public class SWF extends ParserPrototype{
		
		protected static const TWIPS_PER_PIXEL	:uint = 20;
		
		protected var $compressed		:Boolean = false;
		protected var $frameSize		:Rectangle;
		protected var $version			:uint;
		protected var $size				:uint;
		protected var $frameRate		:Number;
		protected var $frameCount		:uint;
		
/*
		  ___ ___  _  _ ___ _____ ___ _   _  ___ _____ ___  ___ 
		 / __/ _ \| \| / __|_   _| _ \ | | |/ __|_   _/ _ \| _ \
		| (_| (_) | .` \__ \ | | |   / |_| | (__  | || (_) |   /
		 \___\___/|_|\_|___/ |_| |_|_\\___/ \___| |_| \___/|_|_\
		C O N S T R U C T O R
*/
		/**
		 * Creates a new SWF object.
		 * 
		 * @param	fileObject		FileObject to parse
		 */
		public function SWF(fileObject:FileObject = null) {
			super(fileObject);
		}
		
		override protected function parse():void {
			super.parse();
			little_endian;
			$compressed = (String.fromCharCode(uint8) == "C");
			trace("compressed: " + $compressed);
			var tokens:String = "";
			tokens += String.fromCharCode(uint8);
			tokens += String.fromCharCode(uint8);
			trace("tokens: " + tokens);
			
		
			
			$version = uint8;
			trace("version: " + $version);
			
			$size = uint32;
			trace("length: " + Number($size/1024).toFixed(3) + "kb");
			
			if (compressed) {
				var ba:ByteArray = new ByteArray();
				bytearray.readBytes(ba);
				ba.uncompress();
				bytearray = ba;
			}
			
			$frameSize = rect;
			
			$frameRate = uint8;
			trace("frameRate: " + $frameRate);
			$frameCount = uint8;
			trace("frameCount: " + $frameCount);
			
		}
		
/*
		  ___ ___ _____   ___ ___ _____ 
		 / __| __|_   _| / __| __|_   _|
		| (_ | _|  | |   \__ \ _|  | |  
		 \___|___| |_|   |___/___| |_|  
		G E T  S E T
*/
		
		private function get rect():Rectangle {
			var len:Number = readUnsignedBits(5);
			var r:Rectangle = new Rectangle();
			r.x 		= int(readBits(len)/TWIPS_PER_PIXEL);
			r.width 	= int(readBits(len)/TWIPS_PER_PIXEL)-r.x;
			r.y 		= int(readBits(len)/TWIPS_PER_PIXEL);
			r.height 	= int(readBits(len)/TWIPS_PER_PIXEL)-r.y;
			
			//TODO confirm that position in bytearray is accurate if bit_buffer is 4 or 7.
			bytearray.position = Math.ceil((5 + 4 * len) / 8)+1;
			
			return r;
			
		}
		
		public function get compressed():Boolean {
			return $compressed;
		}
		
		public function get frameSize():Rectangle {
			return $frameSize;
		}
		
		public function get version():uint {
			return $version;
		}
		
		public function get size():uint {
			return $size;
		}
		
		public function get frameRate():Number {
			return $frameRate;
		}
		
		public function get frameCount():uint {
			return $frameCount;
		}
		
	}

}

